package com.woniuxy.core.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

    public static boolean compareDataAndString(String data1,String data2){

        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");

        try {
            Date d1 = sdf.parse(data1);
            Date d2 = sdf.parse(data2);
            if(d1.getTime()>d2.getTime()){
                return true;
            }else {
                return false;
            }
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }

    }


    public static boolean compareDateWithNow(String data) {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Date now=new Date();
        Date date = null;
        try {
            date = sdf.parse(data);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        if (date.getTime()<=now.getTime()){
            return true;
        }else {
            return false;
        }

    }

    public static boolean isAdult(String idCardNumber) {
        String birthdayString = idCardNumber.substring(6, 14);
        LocalDate birthday = LocalDate.parse(birthdayString, DateTimeFormatter.BASIC_ISO_DATE);
        LocalDate today = LocalDate.now();

        return birthday.plusYears(18).isBefore(today);
    }

    public static String date2String(Date date){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return  sdf.format(date);
    }
    public static Date string2Date(String date) throws ParseException {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        return sdf.parse(date);
    }
    public static String date2dateString(Date date){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        return  sdf.format(date);
    }

    public static String getYesterdayStringDate() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -1);  // 获取前一天的日期
        Date yesterday = calendar.getTime();  // 转换为 Date 类型
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  // 设置日期格式
        return sdf.format(yesterday);
    }

    public static String getTomorrowStringDate() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, +1);  // 获取明天的日期
        Date yesterday = calendar.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(yesterday);
    }
    public static String getTomorrowStringDate(String dateString) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Date date = format.parse(dateString);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.DAY_OF_YEAR, 1);
            Date newDate = calendar.getTime();
            return format.format(newDate);
    }
    public static LocalTime StringToLocalTime(String time){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalTime dateTime = LocalTime.parse(time, formatter);
        return dateTime;
    }

}
